package controllers;

import java.util.ArrayList;
import java.util.List;


import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;

import model.entities.UnmappedItem;
import myDaos.*;


@ManagedBean(name="unmappedItemsBean")
@ViewScoped
public class UnmappedItemsBean {

	
	private List<UnmappedItem> unmappedItemList ;
	private String country;
	private List<SelectItem> distList;
	private boolean isMethodCalled = false;
	private String distributor;
	private List<String> distributors;
	
	
	@EJB
	private UnmappedItemsDao unmappedItemsDao;
	
	@ManagedProperty("#{servicesManagedBean}")
	private ServicesManagedBean servicesManagedBean;
	
	
	public List<String> getDistributors() {
		return distributors;
	}


	public void setDistributors(List<String> distributors) {
		this.distributors = distributors;
	}


	public String getDistributor() {
		return distributor;
	}


	public void setDistributor(String distributor) {
		this.distributor = distributor;
	}


	public List<SelectItem> getDistList() {
		return distList;
	}


	public void setDistList(List<SelectItem> distList) {
		this.distList = distList;
	}


	public ServicesManagedBean getServicesManagedBean() {
		return servicesManagedBean;
	}


	public void setServicesManagedBean(ServicesManagedBean servicesManagedBean) {
		this.servicesManagedBean = servicesManagedBean;
	}

	
	public void setMethodCalled(boolean isMethodCalled) {
		this.isMethodCalled = isMethodCalled;
	}

	
	public String getCountry() {
		return country;
	}


	public void setCountry(String country) {
		this.country = country;
	}


	public UnmappedItemsDao getUnmappedItemsDao() {
		return unmappedItemsDao;
	}


	public void setUnmappedItemsDao(UnmappedItemsDao unmappedItemsDao) {
		this.unmappedItemsDao = unmappedItemsDao;
	}
	
	
	public List<UnmappedItem> getUnmappedItemList() {
		return unmappedItemList;
	}


	public void setUnmappedItemList(List<UnmappedItem> unmappedItemList) {
		this.unmappedItemList = unmappedItemList;
	}
	
	
	public void updateList(){
		/*StringBuffer sb = new StringBuffer();
		if(distributors.size() > 1 ){
			for(int i=0; i< distributors.size(); i++){
				sb.append(distributors.get(i));
				if(!(i == distributors.size()-1)){
					sb.append(',');
				}
			}
		}
		else if(distributors.size() == 1){
			sb.append(distributors.get(0));
		}
		else{
			sb.append("");
		}*/
		unmappedItemList= unmappedItemsDao.getUnmappedItems(country,distributor);
		isMethodCalled=true;
	}
	

	public boolean isMethodCalled() {
		return isMethodCalled;
	}
	
	public void updateDistributorsList()
	{
		//Proposed by Samy
		distList = new ArrayList<SelectItem>();
		for(int i = 0; i < servicesManagedBean.getCountriesDistributorList().size(); i++) 
	    {  
			if(country!=null && !country.trim().equals("") && 
					  servicesManagedBean.getCountriesDistributorList().get(i).getCountry().equals(country))
			{
			distList.add(new SelectItem(
										 servicesManagedBean.getCountriesDistributorList().get(i).getDistCode()
										 ,
										 servicesManagedBean.getCountriesDistributorList().get(i).getDistCode()
					                    )
			             );
			}
	    }
	}
	
}
